<?php

namespace plugin\webman\houwang;
use support\Db;
use GatewayWorker\Lib\Gateway;
use support\Redis;
use plugin\webman\houwang\Method;
use plugin\webman\common\Common;
class Houwang
{

    // 定时器发送状态
    public static function timer()
    { 

        $time = Method::getdaojishi();
        if($time>20){
            $data=[
                'code'=>1,
                'msg'=>'runaway timeout',
                'info'=>Method::getSeconds($time)
            ];
            Gateway::sendToAll(Common::returndata('runawaytime',$data));
        }else{
            // 低于20秒的开奖逻辑， 是逃跑还是让某人中奖
        }
        
        self::robotbet();
    }
    
    // 机器人投注
    public static function robotbet(){
        $robots = Db::table('wa_users')->where('isrobot',1)->get()->map(function ($value) {return (array)$value;})
        ->toArray();
        // $rand = rand(2,4);
        $rand = 2;
        $keys = array_rand($robots,$rand);

        foreach($keys as $key =>$v){
            $new[]=$robots[$v];
          
        }

        foreach($new as $k=>$vt){
              // 分别投注
              $houwang = Method::getmokeyinfo();
              $ruleid = rand(1,3);
              $rule = Method::getruleByid($ruleid);
              $record = [
                  'uid'=>$vt['id'],
                  'score'=>$rule->score,
                  'blood'=>$rule->blood,
                  'houwang_id'=>$houwang['houwang_id'],
                  'expect'=>$houwang['expect'],
                  'createtime'=>date('Y-m-d H:i:s',time()),
                  'isrobot'=>1
              ];
              $in = Db::table('wa_houwang_record')->insert($record);
              if($in){
  
                      // 扣除猴王血量
                      $newblood = Method::upmokeyblood($houwang['houwang_id'],$rule->blood);
                      // 增加猴王逃跑时间
                      $addtime = Method::addmokeytime($houwang['expect'],$houwang['houwang_id']);
                      // 发socket 广播   猴王血量减少，奖池增加
                      $houwang = Method::getmokeyinfo();
                      $houwang['hurtblood']=$rule->blood;
                      $houwang['user']= Common::getuserinfo($vt['id']);
                      $data1 = [
                          'code'=>1,
                          'msg'=>'mokey data update',
                          'info'=>$houwang
                      ];
      
                      Gateway::sendToAll(Common::returndata('mokeyup',$data1));
              }
        }
        
    }
    
    // 进入房间
    public static function enterroom($client_id,$data){
        // 获取当前猴王信息
        $info['houwang'] = Method::getmokeyinfo();
        $info['user'] = Common::getuserinfo($data['uid']);
        $info['rules'] = Method::getrules();

        $data = [
            'code'=>1,
            'msg'=>'进入房间成功',
            'info'=>$info
        ];
        return $data;
    }

    // 投注宝石
    public static function touzhu($client_id,$data){
        $houwang = Method::getmokeyinfo();
        $rule = Method::getruleByid($data['ruleid']);
        $userinfo = Common::getuserinfo($data['uid']);
        if($userinfo['coin']<$rule->score){
            $data = [
                'code'=>1154,
                'msg'=>'宝石不足',
                'info'=>[]
            ];
            return $data;
        }
        $record = [
            'uid'=>$data['uid'],
            'score'=>$rule->score,
            'blood'=>$rule->blood,
            'houwang_id'=>$houwang['houwang_id'],
            'expect'=>$houwang['expect'],
            'createtime'=>date('Y-m-d H:i:s',time()),
        ];
        $in = Db::table('wa_houwang_record')->insert($record);
        if($in){
            $up = Common::updateusercoin($data['uid'],$rule->score,0,'投注'.$houwang['name']);
            $info['user'] = Common::getuserinfo($data['uid']);
            if($up){
                
                // 扣除猴王血量
                $newblood = Method::upmokeyblood($houwang['houwang_id'],$rule->blood);
                // 增加猴王逃跑时间
                $addtime = Method::addmokeytime($houwang['expect'],$houwang['houwang_id']);
                // 发socket 广播   猴王血量减少，奖池增加
                $houwang = Method::getmokeyinfo();
                $houwang['user']= $info['user'];
                $houwang['hurtblood']=$rule->blood;
                $data1 = [
                    'code'=>1,
                    'msg'=>'mokey data update',
                    'info'=>$houwang
                ];

                Gateway::sendToAll(Common::returndata('mokeyup',$data1));


                $data = [
                    'code'=>1,
                    'msg'=>'投入成功',
                    'info'=>$info
                ];
                return $data;
            }else{
                $data = [
                    'code'=>1001,
                    'msg'=>'投入失败',
                    'info'=>$info
                ];
                return $data;
            }
        }else{
            $data = [
                'code'=>1002,
                'msg'=>'投入失败',
                'info'=>[]
            ];
            return $data;
        }
    }


    // 获取排行
    public static function paihang(){
        $list = Db::table("wa_houwang_paihang")->orderByDesc("rewards",true)->get()->map(function ($value) {return (array)$value;})
        ->toArray();
        if($list){
            foreach($list as $k =>$v){
                $v['userinfo']=Common::getuserinfo($v['uid']);
                $new[]=$v;
            }
        }else{
            $new = [];
        }
        $data = [
            'code'=>1,
            'msg'=>'success',
            'info'=>$new
        ];
        return $data;
        
    }

    // 获取奖池
    public static function jiangchi($client_id,$data){
        $info['houwang'] = Method::getmokeyinfo();
        // 炸弹总人数
        $info['allpeople'] = Db::table("wa_houwang_record")->where("houwang_id",$info['houwang']['houwang_id'])->count();
        // 炸弹数量
        $info['allbet'] =  Db::table("wa_houwang_record")->where("houwang_id",$info['houwang']['houwang_id'])->sum("score");
        // 猴王逃跑每个炸弹瓜分额

        // var_dump($info['houwang']['lastreward'],$info['allbet']   );die;
        $info['pingjunguafen']=$info['houwang']['lastreward2']/$info['allbet'];   
        $list =  Db::table("wa_houwang_record")->where("houwang_id",$info['houwang']['houwang_id'])->get()->map(function ($value) {return (array)$value;})
        ->toArray();
        if($list){
            foreach($list as $k =>$v){
                $v['userinfo']=Common::getuserinfo($v['uid']);
                $new[]=$v;
            }
        }else{
            $new=[];
        }
        
        $data = [
            'code'=>1,
            'msg'=>'success',
            'info'=>$new
        ];
        return $data;

    }

}